home *** CD-ROM | disk | FTP | other *** search
/ isnet Internet / Isnet Internet CD.iso / prog / hiz / 09 / 09.exe / adynware.exe / perl / lib / AutoSplit.pm < prev    next >
Encoding:
Perl POD Document  |  1999-12-28  |  10.3 KB  |  327 lines

  1. package AutoSplit;
  2.  
  3. require 5.000;
  4. require Exporter;
  5.  
  6. use Config;
  7. use Carp;
  8. use File::Path qw(mkpath);
  9.  
  10. @ISA = qw(Exporter);
  11. @EXPORT = qw(&autosplit &autosplit_lib_modules);
  12. @EXPORT_OK = qw($Verbose $Keep $Maxlen $CheckForAutoloader $CheckModTime);
  13.  
  14. =head1 NAME
  15.  
  16. AutoSplit - split a package for autoloading
  17.  
  18. =head1 SYNOPSIS
  19.  
  20.  perl -e 'use AutoSplit; autosplit_lib_modules(@ARGV)' ...
  21.  
  22.  use AutoSplit; autosplit($file, $dir, $keep, $check, $modtime);
  23.  
  24. for perl versions 5.002 and later:
  25.  
  26.  perl -MAutoSplit -e 'autosplit($ARGV[0], $ARGV[1], $k, $chk, $modtime)' ...
  27.  
  28. =head1 DESCRIPTION
  29.  
  30. This function will split up your program into files that the AutoLoader
  31. module can handle. It is used by both the standard perl libraries and by
  32. the MakeMaker utility, to automatically configure libraries for autoloading.
  33.  
  34. The C<autosplit> interface splits the specified file into a hierarchy 
  35. rooted at the directory C<$dir>. It creates directories as needed to reflect
  36. class hierarchy, and creates the file F<autosplit.ix>. This file acts as
  37. both forward declaration of all package routines, and as timestamp for the
  38. last update of the hierarchy.
  39.  
  40. The remaining three arguments to C<autosplit> govern other options to the
  41. autosplitter. If the third argument, I<$keep>, is false, then any pre-existing
  42. C<*.al> files in the autoload directory are removed if they are no longer
  43. part of the module (obsoleted functions). The fourth argument, I<$check>,
  44. instructs C<autosplit> to check the module currently being split to ensure
  45. that it does include a C<use> specification for the AutoLoader module, and
  46. skips the module if AutoLoader is not detected. Lastly, the I<$modtime>
  47. argument specifies that C<autosplit> is to check the modification time of the
  48. module against that of the C<autosplit.ix> file, and only split the module
  49. if it is newer.
  50.  
  51. Typical use of AutoSplit in the perl MakeMaker utility is via the command-line
  52. with:
  53.  
  54.  perl -e 'use AutoSplit; autosplit($ARGV[0], $ARGV[1], 0, 1, 1)'
  55.  
  56. Defined as a Make macro, it is invoked with file and directory arguments;
  57. C<autosplit> will split the specified file into the specified directory and
  58. delete obsolete C<.al> files, after checking first that the module does use
  59. the AutoLoader, and ensuring that the module is not already currently split
  60. in its current form (the modtime test).
  61.  
  62. The C<autosplit_lib_modules> form is used in the building of perl. It takes
  63. as input a list of files (modules) that are assumed to reside in a directory
  64. B<lib> relative to the current directory. Each file is sent to the 
  65. autosplitter one at a time, to be split into the directory B<lib/auto>.
  66.  
  67. In both usages of the autosplitter, only subroutines defined following the
  68. perl special marker I<__END__> are split out into separate files. Some
  69. routines may be placed prior to this marker to force their immediate loading
  70. and parsing.
  71.  
  72. =head1 CAVEATS
  73.  
  74. Currently, C<AutoSplit> cannot handle multiple package specifications
  75. within one file.
  76.  
  77. =head1 DIAGNOSTICS
  78.  
  79. C<AutoSplit> will inform the user if it is necessary to create the top-level
  80. directory specified in the invocation. It is preferred that the script or
  81. installation process that invokes C<AutoSplit> have created the full directory
  82. path ahead of time. This warning may indicate that the module is being split
  83. into an incorrect path.
  84.  
  85. C<AutoSplit> will warn the user of all subroutines whose name causes potential
  86. file naming conflicts on machines with drastically limited (8 characters or
  87. less) file name length. Since the subroutine name is used as the file name,
  88. these warnings can aid in portability to such systems.
  89.  
  90. Warnings are issued and the file skipped if C<AutoSplit> cannot locate either
  91. the I<__END__> marker or a "package Name;"-style specification.
  92.  
  93. C<AutoSplit> will also emit general diagnostics for inability to create
  94. directories or files.
  95.  
  96. =cut
  97.  
  98. $Maxlen  = 8;    # 8 for dos, 11 (14-".al") for SYSVR3
  99. $Verbose = 1;    # 0=none, 1=minimal, 2=list .al files
  100. $Keep    = 0;
  101. $CheckForAutoloader = 1;
  102. $CheckModTime = 1;
  103.  
  104. $IndexFile = "autosplit.ix";    # file also serves as timestamp
  105. $maxflen = 255;
  106. $maxflen = 14 if $Config{'d_flexfnam'} ne 'define';
  107. $Is_VMS = ($^O eq 'VMS');
  108.  
  109.  
  110. sub autosplit{
  111.     my($file, $autodir,  $k, $ckal, $ckmt) = @_;
  112.     $keep = $Keep unless defined $k;
  113.     $ckal = $CheckForAutoloader unless defined $ckal;
  114.     $ckmt = $CheckModTime unless defined $ckmt;
  115.     autosplit_file($file, $autodir, $keep, $ckal, $ckmt);
  116. }
  117.  
  118.  
  119.  
  120. sub autosplit_lib_modules{
  121.     my(@modules) = @_; # list of Module names
  122.  
  123.     while(defined($_ = shift @modules)){
  124.     s#::#/#g;    # incase specified as ABC::XYZ
  125.     s|\\|/|g;        # bug in ksh OS/2
  126.     s#^lib/##; # incase specified as lib/*.pm
  127.     if ($Is_VMS && /[:>\]]/) { # may need to convert VMS-style filespecs
  128.         my ($dir,$name) = (/(.*])(.*)/);
  129.         $dir =~ s/.*lib[\.\]]//;
  130.         $dir =~ s#[\.\]]#/#g;
  131.         $_ = $dir . $name;
  132.     }
  133.     autosplit_file("lib/$_", "lib/auto", $Keep, $CheckForAutoloader, $CheckModTime);
  134.     }
  135.     0;
  136. }
  137.  
  138.  
  139.  
  140. sub autosplit_file{
  141.     my($filename, $autodir, $keep, $check_for_autoloader, $check_mod_time) = @_;
  142.     my(@names);
  143.     local($_);
  144.  
  145.     $autodir = "lib/auto" unless $autodir;
  146.     if ($Is_VMS) {
  147.     ($autodir = VMS::Filespec::unixpath($autodir)) =~ s{/$}{};
  148.     $filename = VMS::Filespec::unixify($filename); # may have dirs
  149.     }
  150.     unless (-d $autodir){
  151.     mkpath($autodir,0,0755);
  152.     print "Warning: AutoSplit had to create top-level $autodir unexpectedly.\n";
  153.     }
  154.  
  155.     $filename .= ".pm" unless ($filename =~ m/\.pm$/);
  156.  
  157.     open(IN, "<$filename") || die "AutoSplit: Can't open $filename: $!\n";
  158.     my($pm_mod_time) = (stat($filename))[9];
  159.     my($autoloader_seen) = 0;
  160.     my($in_pod) = 0;
  161.     while (<IN>) {
  162.     $in_pod = 1 if /^=/;
  163.     $in_pod = 0 if /^=cut/;
  164.     next if ($in_pod || /^=cut/);
  165.  
  166.     $package = $1 if (m/^\s*package\s+([\w:]+)\s*;/);
  167.     ++$autoloader_seen if m/^\s*(use|require)\s+AutoLoader\b/;
  168.     ++$autoloader_seen if m/\bISA\s*=.*\bAutoLoader\b/;
  169.     last if /^__END__/;
  170.     }
  171.     if ($check_for_autoloader && !$autoloader_seen){
  172.     print "AutoSplit skipped $filename: no AutoLoader used\n" if ($Verbose>=2);
  173.     return 0
  174.     }
  175.     $_ or die "Can't find __END__ in $filename\n";
  176.  
  177.     $package or die "Can't find 'package Name;' in $filename\n";
  178.  
  179.     my($modpname) = $package; 
  180.     if ($^O eq 'MSWin32') {
  181.     $modpname =~ s#::#\\#g; 
  182.     } else {
  183.     $modpname =~ s#::#/#g;
  184.     }
  185.  
  186.     die "Package $package ($modpname.pm) does not match filename $filename"
  187.         unless ($filename =~ m/\Q$modpname.pm\E$/ or
  188.             ($^O eq "msdos") or ($^O eq 'MSWin32') or
  189.                 $Is_VMS && $filename =~ m/$modpname.pm/i);
  190.  
  191.     my($al_idx_file) = "$autodir/$modpname/$IndexFile";
  192.  
  193.     if ($check_mod_time){
  194.     my($al_ts_time) = (stat("$al_idx_file"))[9] || 1;
  195.     if ($al_ts_time >= $pm_mod_time){
  196.         print "AutoSplit skipped ($al_idx_file newer that $filename)\n"
  197.         if ($Verbose >= 2);
  198.         return undef;    # one undef, not a list
  199.     }
  200.     }
  201.  
  202.     my($from) = ($Verbose>=2) ? "$filename => " : "";
  203.     print "AutoSplitting $package ($from$autodir/$modpname)\n"
  204.     if $Verbose;
  205.  
  206.     unless (-d "$autodir/$modpname"){
  207.     mkpath("$autodir/$modpname",0,0777);
  208.     }
  209.  
  210.  
  211.  
  212.     open(OUT,">/dev/null") || open(OUT,">nla0:"); # avoid 'not opened' warning
  213.     my(@subnames, %proto);
  214.     my @cache = ();
  215.     my $caching = 1;
  216.     while (<IN>) {
  217.     next if /^=\w/ .. /^=cut/;
  218.     if (/^package ([\w:]+)\s*;/) {
  219.         warn "package $1; in AutoSplit section ignored. Not currently supported.";
  220.     }
  221.     if (/^sub\s+([\w:]+)(\s*\(.*?\))?/) {
  222.         print OUT "1;\n";
  223.         my $subname = $1;
  224.         $proto{$1} = $2 || '';
  225.         if ($subname =~ m/::/){
  226.         warn "subs with package names not currently supported in AutoSplit section";
  227.         }
  228.         push(@subnames, $subname);
  229.         my($lname, $sname) = ($subname, substr($subname,0,$maxflen-3));
  230.         my($lpath) = "$autodir/$modpname/$lname.al";
  231.         my($spath) = "$autodir/$modpname/$sname.al";
  232.         unless(open(OUT, ">$lpath")){
  233.         open(OUT, ">$spath") or die "Can't create $spath: $!\n";
  234.         push(@names, $sname);
  235.         print "  writing $spath (with truncated name)\n"
  236.             if ($Verbose>=1);
  237.         }else{
  238.         push(@names, $lname);
  239.         print "  writing $lpath\n" if ($Verbose>=2);
  240.         }
  241.         print OUT "# NOTE: Derived from $filename.  ",
  242.             "Changes made here will be lost.\n";
  243.         print OUT "package $package;\n\n";
  244.         print OUT @cache;
  245.         @cache = ();
  246.         $caching = 0;
  247.     }
  248.     if($caching) {
  249.         push(@cache, $_) if @cache || /\S/;
  250.     }
  251.     else {
  252.         print OUT $_;
  253.     }
  254.     if(/^}/) {
  255.         if($caching) {
  256.         print OUT @cache;
  257.         @cache = ();
  258.         }
  259.         print OUT "\n";
  260.         $caching = 1;
  261.     }
  262.     }
  263.     print OUT @cache,"1;\n";
  264.     close(OUT);
  265.     close(IN);
  266.  
  267.     if (!$keep){  # don't keep any obsolete *.al files in the directory
  268.     my(%names);
  269.     @names{@names} = @names;
  270.     opendir(OUTDIR,"$autodir/$modpname");
  271.     foreach(sort readdir(OUTDIR)){
  272.         next unless /\.al$/;
  273.         my($subname) = m/(.*)\.al$/;
  274.         next if $names{substr($subname,0,$maxflen-3)};
  275.         my($file) = "$autodir/$modpname/$_";
  276.         print "  deleting $file\n" if ($Verbose>=2);
  277.         my($deleted,$thistime);  # catch all versions on VMS
  278.         do { $deleted += ($thistime = unlink $file) } while ($thistime);
  279.         carp "Unable to delete $file: $!" unless $deleted;
  280.     }
  281.     closedir(OUTDIR);
  282.     }
  283.  
  284.     open(TS,">$al_idx_file") or
  285.     carp "AutoSplit: unable to create timestamp file ($al_idx_file): $!";
  286.     print TS "# Index created by AutoSplit for $filename (file acts as timestamp)\n";
  287.     print TS "package $package;\n";
  288.     print TS map("sub $_$proto{$_} ;\n", @subnames);
  289.     print TS "1;\n";
  290.     close(TS);
  291.  
  292.     check_unique($package, $Maxlen, 1, @names);
  293.  
  294.     @names;
  295. }
  296.  
  297.  
  298. sub check_unique{
  299.     my($module, $maxlen, $warn, @names) = @_;
  300.     my(%notuniq) = ();
  301.     my(%shorts)  = ();
  302.     my(@toolong) = grep(length > $maxlen, @names);
  303.  
  304.     foreach(@toolong){
  305.     my($trunc) = substr($_,0,$maxlen);
  306.     $notuniq{$trunc}=1 if $shorts{$trunc};
  307.     $shorts{$trunc} = ($shorts{$trunc}) ? "$shorts{$trunc}, $_" : $_;
  308.     }
  309.     if (%notuniq && $warn){
  310.     print "$module: some names are not unique when truncated to $maxlen characters:\n";
  311.     foreach(keys %notuniq){
  312.         print " $shorts{$_} truncate to $_\n";
  313.     }
  314.     }
  315.     %notuniq;
  316. }
  317.  
  318. 1;
  319. __END__
  320.  
  321. sub test1{ "test 1\n"; }
  322. sub test2{ "test 2\n"; }
  323. sub test3{ "test 3\n"; }
  324. sub test4{ "test 4\n"; }
  325.  
  326.  
  327.